﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using MessageHandler;
using Msg = MessageHandler.Message;
using PluginHost;

namespace Chatclient
{
	public partial class FormPrivateChat : Form
	{
        private Controller controller;

        public Controller GetController()
        {
            return controller;
        }
        
		private PrivateChatMembers chatMembers;


		public PrivateChatMembers ChatMembers
		{
			get { return chatMembers; }
			set { chatMembers = value; }
		}

		public UserInfo userInfo;

		public FormPrivateChat(Controller controller, UserInfo[] group, UserInfo thisUser)
			: this()
		{
			this.controller = controller;
			this.userInfo = thisUser;
			chatMembers = new PrivateChatMembers(group, thisUser);
		}

		private FormPrivateChat()
        {
            InitializeComponent();

            foreach (PluginHost.Types.AvailablePlugin pluginOn in Global.Plugins.AvailablePlugins)
            {
                pluginOn.Instance.addFunctionality(this);
            }


			this.ActiveControl = txtBxInput;


		}


		#region Events

		private void FormPrivateChat_Load(object sender, EventArgs e)
		{
			this.Text = chatMembers.ToString();
			lstBxGroup.Items.AddRange(chatMembers.UserInfos.ToArray());
			lblUser.Text = userInfo.ToString();
		}

		private void btnSend_Click(object sender, EventArgs e)
		{
			SendPrivateMessage();
		}

		private void txtBxInput_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter && !e.Shift)
			{
				SendPrivateMessage();
			}
		}

		private void txtBxChat_LinkClicked(object sender, LinkClickedEventArgs e)
		{
			System.Diagnostics.Process.Start(e.LinkText);
		}

		#endregion

		#region Work

		private void SendPrivateMessage()
		{
			if (txtBxInput.TextLength == 0)
			{
				return;
			}
			controller.SendPrivateMessage(txtBxInput.Text, this.chatMembers.UserInfos.ToArray());
			Application.DoEvents();
			txtBxInput.Clear();
		}


		internal void WriteMessage(Msg message)
		{
			PrivateMessageType type = (PrivateMessageType)message.Item;
			this.AppendChatText(type.from.ToString(), message.text);
		}

		private delegate void AppendChatTextCallback(string head, string message);
		public void AppendChatText(string head, string message)
		{
			if (this.InvokeRequired)
			{
				AppendChatTextCallback acc = new AppendChatTextCallback(AppendChatText);
				this.Invoke(acc, head, message);
			}
			else
			{
                DateTime dateTime = DateTime.Now;
				txtBxChat.SelectionStart = txtBxChat.TextLength;
				txtBxChat.SelectionLength = 0;
				txtBxChat.SelectionColor = Color.Blue;
				txtBxChat.SelectionFont = new System.Drawing.Font("Microsoft Sans Serif", 9, FontStyle.Bold);
				txtBxChat.AppendText("[" + dateTime.ToShortTimeString() + "] ");
				txtBxChat.AppendText(head + ":" + Environment.NewLine);
				txtBxChat.SelectionColor = Color.Black;
				txtBxChat.SelectionFont = new System.Drawing.Font("Microsoft Sans Serif", 8.5f, FontStyle.Regular);
				txtBxChat.AppendText(message + Environment.NewLine + Environment.NewLine);
				txtBxChat.ScrollToCaret();

                string mes = head + "³" + message + "³" + dateTime.ToString("dd.MM.yyyy HH:mm");
                foreach (PluginHost.Types.AvailablePlugin pluginOn in Global.Plugins.AvailablePlugins)
                {
                    pluginOn.Instance.handleMessage(null, mes);
                }
			}
		}

		#endregion


	}
}
